package scicalc;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Collection {

    /**
     * a ∩ b
     *
     * @param a
     * @param b
     * @return
     */
    public static List<Integer> intersection(List<Integer> a, List<Integer> b) {
        List<Integer> rs = new ArrayList<>();
        Set<Integer> set = new HashSet<>(b);
        for (Integer i : a)
            if (set.contains(i))
                rs.add(i);
        return rs;
    }

    /**
     * a ∪ b
     *
     * @param a
     * @param b
     * @return
     */
    public static List<Integer> union(List<Integer> a, List<Integer> b) {
        Set<Integer> set = new HashSet<>();
        set.addAll(a);
        set.addAll(b);
        return new ArrayList<>(set);
    }

    /**
     * a - b
     *
     * @param a
     * @param b
     * @return
     */
    public static List<Integer> difference(List<Integer> a, List<Integer> b) {
        Set<Integer> set = new HashSet<>(a);
        set.removeAll(b);
        return new ArrayList<>(set);
    }

    /**
     * a - {b}
     *
     * @param a
     * @param b
     * @return
     */
    public static List<Integer> difference(List<Integer> a, int b) {
        Set<Integer> set = new HashSet<>(a);
        set.remove(b);
        return new ArrayList<>(set);
    }
}
